from django.db import models


class ZMADRouter:
    """
    A router to control all database operations on models in the
    zmad application.
    """
    def db_for_read(self, model, **hints):
        if model._meta.app_label == 'zmad':
            return 'zmad'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label == 'zmad':
            # 确保zmad中的数据是只读的
            # raise models.ProtectedError('Model %s is read-only.' % model.__class__.__name__, model)
            return "zmad"
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._meta.app_label == 'zmad' or \
           obj2._meta.app_label == 'zmad':
           return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        # 确保zmad数据库不被修改
        if db == 'zmad':
            return False
        # 其他场景不处理
        return None
